package view.dtvb;

import java.util.List;

import javax.faces.event.ActionEvent;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewObject;

import view.common.Utils;

public class dmdonviBean {
    private RichPopup popupThemMoi;
    private RichPopup popupXem;
    private RichPopup popupDelete;
    private RichPopup popupValidate;
    private String Tendonvi;
    private String Title;
    private RichOutputText inputtextOperator;
    private RichInputText txtTendonvi = new RichInputText();

    public dmdonviBean() {
        super();
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            DCIteratorBinding iterDmDonVi =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");
            iterDmDonVi.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmDonVi.executeQuery();

            DCIteratorBinding iterDmDonviCheckExist =
                Utils.getIteratorBinding("DtvbDonviHienthiView1Iterator");
            iterDmDonviCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmDonviCheckExist.executeQuery();
        }
    }

    public void ResetSearch() {
        try {
            DCIteratorBinding iterDmDonVi =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");
            iterDmDonVi.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmDonVi.executeQuery();

            DCIteratorBinding iterDmDonviCheckExist =
                Utils.getIteratorBinding("DtvbDonviHienthiView1Iterator");
            iterDmDonviCheckExist.getViewObject().getViewCriteriaManager().clearViewCriterias();
            iterDmDonviCheckExist.executeQuery();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void create(ActionEvent actionEvent) {
        Utils.execute("CreateInsert");
        Utils.showPopup(popupThemMoi);
        inputtextOperator.setValue("0");
    }

    public void update(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("DtvbDonviView1Iterator").getCurrentRow() ==
            null) {
            String meg = "Chưa có đơn vị nào.";
            Utils.showMessage(Utils.MSG_INFO, meg);
        } else {
            Utils.showPopup(popupThemMoi);
            inputtextOperator.setValue("1");
        }
    }

    public int checkExisted(String Tendonvi, String DonviId) {
        DCIteratorBinding IteratorDmDonvi =
            Utils.getIteratorBinding("DtvbDonviHienthiView1Iterator");
        ViewObject vo = IteratorDmDonvi.getViewObject();
        ViewCriteria vc =
            vo.getViewCriteriaManager().getViewCriteria("DtvbDonviHienthiViewCriteria");
        vo.ensureVariableManager().setVariableValue("varTendonvi", Tendonvi);
        vo.ensureVariableManager().setVariableValue("varCheckId", DonviId);
        vo.getViewCriteriaManager().removeViewCriteria("DtvbDonviHienthiViewCriteria");
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.setRangeSize(-1);
        IteratorDmDonvi.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            return 1;
        }
        return 0;
    }

    public void commitDialog(DialogEvent dialogEvent) {
        try {
            int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
            DCIteratorBinding iterDmDonvi =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");
            String DonviId = "";
            String TendonviMoi = "";
            TendonviMoi = txtTendonvi.getValue().toString();
            if (operator == 0) {
                DonviId = "";
            } else {
                DonviId =
                        iterDmDonvi.getCurrentRow().getAttribute("Id").toString();
            }
            if (checkExisted(TendonviMoi, DonviId) > 0) {
                String meg =
                    "Tên đơn vị đã tồn tại. Bạn phải nhập tên đơn vị khác";
                Utils.showMessage(Utils.MSG_ERROR, meg);
            } else {
                String rowKey = "";
                if (iterDmDonvi.getCurrentRow() != null) {
                    iterDmDonvi.getCurrentRow().getKey().toStringFormat(false);
                }
                Utils.execute("Commit");
                Utils.iteratorRefresh("DtvbDonviView1Iterator");
                Utils.iteratorRefresh("DtvbDonviHienthiView1Iterator");
                iterDmDonvi.setCurrentRowWithKey(rowKey);
                if (operator == 0) {
                    ViewObject vo = iterDmDonvi.getViewObject();
                    ViewCriteria vc =
                        vo.getViewCriteriaManager().getViewCriteria("DtvbDonviViewCriteria");
                    vo.ensureVariableManager().setVariableValue("varTendonvi",
                                                                "%");
                    vo.getViewCriteriaManager().removeViewCriteria("DtvbDonviViewCriteria");
                    vo.getViewCriteriaManager().applyViewCriteria(vc);
                    iterDmDonvi.executeQuery();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void cancelDiaglog(PopupCanceledEvent popupCanceledEvent) {
        DCIteratorBinding iterDmDonvi =
            Utils.getIteratorBinding("DtvbDonviView1Iterator");
        String rowKey = "";
        if (iterDmDonvi.getCurrentRow() != null) {
            rowKey =
                    iterDmDonvi.getCurrentRow().getKey().toStringFormat(false);
        }
        Utils.execute("Rollback");
        iterDmDonvi.setCurrentRowWithKey(rowKey);
    }

    public void deleteDiaglog(DialogEvent dialogEvent) {
        try {
            if (Utils.getIteratorBinding("DtvbDonviView1Iterator").getCurrentRow() ==
                null) {
                String meg = "Chưa có đơn vị nào.";
                Utils.showMessage(Utils.MSG_INFO, meg);
            } else {
                Utils.execute("Delete");
                List constrainExist = Utils.execute("Commit");
                if (constrainExist != null &&
                    constrainExist.toString().contains("NOIDUNGVANBAN_DONVI_FK")) {
                    Utils.showPopup(popupValidate);
                }
                Utils.iteratorRefresh("DtvbDonviView1Iterator");
                Utils.iteratorRefresh("DtvbDonviHienthiView1Iterator");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void searchDmDonvi() {
        try {
            DCIteratorBinding IteratorDonvi =
                Utils.getIteratorBinding("DtvbDonviView1Iterator");
            ViewObject vo = IteratorDonvi.getViewObject();
            ViewCriteria vc =
                vo.getViewCriteriaManager().getViewCriteria("DtvbDonviViewCriteria");
            vo.ensureVariableManager().setVariableValue("varTendonvi",
                                                        getTendonvi());

            vo.getViewCriteriaManager().removeViewCriteria("DtvbDonviViewCriteria");
            vo.getViewCriteriaManager().applyViewCriteria(vc);
            IteratorDonvi.executeQuery();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void openPopupDelete(ActionEvent actionEvent) {
        if (Utils.getIteratorBinding("DtvbDonviView1Iterator").getCurrentRow() ==
            null) {
            String messageText = "Không có đơn vị đơn vị nào.";
            Utils.showMessage(Utils.MSG_INFO, messageText);
        } else {
            Utils.showPopup(popupDelete);
        }
    }

    public void setPopupThemMoi(RichPopup popupThemMoi) {
        this.popupThemMoi = popupThemMoi;
    }

    public RichPopup getPopupThemMoi() {
        return popupThemMoi;
    }

    public void setTendonvi(String Tendonvi) {
        this.Tendonvi = Tendonvi;
    }

    public String getTendonvi() {
        return Tendonvi;
    }

    public void setInputtextOperator(RichOutputText inputtextOperator) {
        this.inputtextOperator = inputtextOperator;
    }

    public RichOutputText getInputtextOperator() {
        return inputtextOperator;
    }

    public void setPopupXem(RichPopup popupXem) {
        this.popupXem = popupXem;
    }

    public RichPopup getPopupXem() {
        return popupXem;
    }

    public void setTitle(String Title) {
        this.Title = Title;
    }

    public String getTitle() {
        Title = "Thêm mới đơn vị";
        int operator = inputtextOperator.getValue().equals("1") ? 1 : 0;
        if (operator == 0) {
            Title = "Thêm mới đơn vị";
        } else {
            Title = "Sửa đơn vị";
        }
        return Title;
    }

    public void setTxtTendonvi(RichInputText txtTendonvi) {
        this.txtTendonvi = txtTendonvi;
    }

    public RichInputText getTxtTendonvi() {
        return txtTendonvi;
    }

    public void setPopupDelete(RichPopup popupDelete) {
        this.popupDelete = popupDelete;
    }

    public RichPopup getPopupDelete() {
        return popupDelete;
    }

    public void setPopupValidate(RichPopup popupValidate) {
        this.popupValidate = popupValidate;
    }

    public RichPopup getPopupValidate() {
        return popupValidate;
    }
}
